package org.pray.activity.responsibility;

import java.util.Optional;

//写一个责任链
public abstract class Handler {
    protected Handler doHandler;

    // 传递
    public Handler connect(Handler handler) {
        this.doHandler = handler;
        return doHandler;
    }

    public void LetHandleTODoRealWork() {
        if (this.doHandler != null) {
            System.out.println("现在的责任链传递到：" + this.doHandler.getClass().getName());
        }
        this.doHandle();
        // 传递给下一层,并且执行相应的具体操作
        Optional.ofNullable(this.doHandler)
                .ifPresent(Handler::LetHandleTODoRealWork);// 消费者方法，传入并接收accept
    }

    public abstract void doHandle(); // 结合上节课学习的模板方法，交给子类实现
}
